package com.leetcode.testcode;

import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yanglin
 * @create 2024-01-05 11:41 星期五
 * description: 罗马数字转整数
 */
public class RomanToInt {


    /**
     * 罗马数字转整数
     *  I     1
     *  V     5
     *  X     10
     *  L     50
     *  C     100
     *  D     500
     *  M     1000
     */
    public int testRomanToInt2(String s){
        //通常情况下：罗马数字中，小的数字在大的数字右边，该字符视为一个单独的数字，即累加每个字符对应的数字即可
        //若大的数字在小的数字左边，则需减去小的数字
        //例：MCMXCIV = 1000 - 100 + 1000 - 10 + 100 - 1 + 5
        Map<Character,Integer> map = new HashMap<>();
        map.put('I',1);
        map.put('V',5);
        map.put('X',10);
        map.put('L',50);
        map.put('C',100);
        map.put('D',500);
        map.put('M',1000);
        int num = 0;
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i != chars.length - 1 && map.get(chars[i]) < map.get(chars[i+1])){
                num -= map.get(chars[i]);
            }else {
                num += map.get(chars[i]);
            }
        }
        return num;
    }
    
    
    
    
    /**
     * 暴力枚举
     */
    public int testRomanToInt(String s){ 
        int num = 0;
        //6中特殊情况
        if (s.contains("IV")){
            num += 4;
            s = s.replaceAll("IV","");
        }
        if (s.contains("IX")){
            num += 9;
            s = s.replaceAll("IX","");
        }
        if (s.contains("XL")){
            num += 40;
            s = s.replaceAll("XL","");
        }
        if (s.contains("XC")){
            num += 90;
            s = s.replaceAll("XC","");
        }
        if (s.contains("CD")){
            num += 400;
            s = s.replaceAll("CD","");
        }
        if (s.contains("CM")){
            num += 900;
            s = s.replaceAll("CM","");
        }
        char[] chars = s.toCharArray();
        
        if (!s.equals("")){
            for (char aChar : chars) {
                if (aChar == 'I'){
                    num += 1;
                }
                if (aChar == 'V'){
                    num += 5;
                }
                if (aChar == 'X'){
                    num += 10;
                }
                if (aChar == 'L'){
                    num += 50;
                }
                if (aChar == 'C'){
                    num += 100;
                }
                if (aChar == 'D'){
                    num += 500;
                }
                if (aChar == 'M'){
                    num += 1000;
                }
            }
        }
        return num;
    }
    
    
    @Test
    public void test(){ 
        System.out.println(this.testRomanToInt2("III"));         //3
        System.out.println(this.testRomanToInt2("IV"));          //4
        System.out.println(this.testRomanToInt2("IX"));         //9
        System.out.println(this.testRomanToInt2("LVIII"));      //58
        System.out.println(this.testRomanToInt2("MCMXCIV"));    //1994
    }
    
    
}
